<template>
  <div>
    <el-table v-loading="loading" :data="dataSource" border @row-click="handleRowClick" :height="autoHeight">
      <el-table-column prop="sourceProp" label="数据源属性" align="center">
        <template slot-scope="scope">
          <el-input v-if="scope.row.editAbled" v-model="scope.row[scope.column.property]" :placeholder="scope.column.label" clearable></el-input>
          <span v-else>{{scope.row[scope.column.property]}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="pluginProp" label="引用插件" align="center">
        <template slot-scope="scope">
          <el-select :disabled="!scope.row.editAbled" v-model="scope.row[scope.column.property]" :placeholder="scope.column.label" style="width: 100%" clearable>
            <el-option v-for="item in plugins" :key="item.value" :value="item.value" :label="item.label" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100px">
        <template slot="header">
          <el-button type="primary" @click="() => { dataSource.splice(0,0,{editAbled:true, templateId: templateId })}" icon="el-icon-circle-plus-outline">新增</el-button>
        </template>
        <div slot-scope="scope">
          <template v-if="select">
            <el-button type="success" icon="el-icon-thumb" circle @click="$emit('rowSelect',scope.row)" title="选择"></el-button>
          </template>
          <template v-else>
            <el-button v-if="scope.row.editAbled" type="warning" icon="el-icon-circle-check" circle @click="saveData(scope.row)" title="保存"></el-button>
            <el-button v-else type="success" icon="el-icon-edit" circle @click="() => { $set(scope.row, 'editAbled', true)}" title="编辑"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle @click="removeData(scope.row, scope.$index)" title="删除"></el-button>
          </template>
        </div>
      </el-table-column>
    </el-table>
    <div class="lab-table-page">
      <div style="display: inline-flex">
        <el-pagination
            :disabled="loading"
            :current-page="pageInfo.page"
            :page-sizes="[5, 10, 15, 20, 50, 100]"
            :page-size="pageInfo.pageSize"
            layout="total, sizes, jumper"
            :total="pageInfo.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
        <el-button-group style="padding: 2px 5px">
          <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />
        </el-button-group>
      </div>
    </div>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";

export default {
  name: "ReportPlugin",
  mixins: [IdeaTable],
  props: {
    select: {
      type: Boolean,
      default: false
    },
    templateId: {
      type: String,
      default: ''
    }
  },
  computed:{
    autoHeight() {
      return `calc(100vh - 405px)`
    }
  },
  data() {
    return {
      plugins: [
        { value: 'LoopRowTableRenderPolicy', label: '循环表格行'}
      ]
    }
  },
  created() {
    this.queryUrl = '/reportPlugin/queryPage'
    this.saveUrl = '/reportPlugin/save'
    this.removeUrl = '/reportPlugin/delete/'
    this.rowKey = 'pluginId'
    this.addFilter({ field: 'templateId', op: '=', value: this.templateId })
    this.pageInfo.sorts.push({field: 'updateAt', asc: false })
    this.loadSource()
  },
  methods: {
  }
}
</script>

<style scoped>

</style>
